package mesweb.mesweb.service.impl.procedure;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mesweb.mesweb.entity.Procedure.MesProductProcedure;
import mesweb.mesweb.mapper.procedure.MesProductProcedureBranchProcedureMapper;
import mesweb.mesweb.mapper.procedure.MesProductProcedureMapper;
import mesweb.mesweb.service.procedure.IMesProductProcedureService;
import mesweb.mesweb.vo.procecdure.MesProductProcedureVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 生产工序
 * @Author: jeecg-boot
 * @Date:   2021-06-26
 * @Version: V1.0
 */
@Service
public class MesProductProcdureServiceImpl extends ServiceImpl<MesProductProcedureMapper, MesProductProcedure> implements IMesProductProcedureService {

    @Autowired
    private MesProductProcedureMapper mesProductProcedureMapper;

    @Autowired
    private MesProductProcedureBranchProcedureMapper mesProductProcedureBranchProcedure;

    @Override
    public IPage<MesProductProcedureVo> getProductProcedure(Page page, QueryWrapper queryWrapper) {
        return mesProductProcedureMapper.getProductProcedure(page,queryWrapper);
    }

    @Override
    public List<MesProductProcedureVo> getProductProcedure(QueryWrapper queryWrapper) {
        return mesProductProcedureMapper.getProductProcedure(queryWrapper);
    }

    @Override
    public int updateBottleneckProcedure(long id) {
        return mesProductProcedureMapper.updateBottleneckProcedure(id);
    }

    @Override
    public HashMap<Long,Integer> dispathProductProducedureQty(String pruductNo, //产品编号
                                                   long qty //计划数量

    ){
        List<MesProductProcedureVo> lst = mesProductProcedureMapper.getProductProcedure(null);
        List<MesProductProcedureVo> lstPp = lst.stream().filter(o->o.getProductNo().equals(pruductNo)).collect(Collectors.toList());

        Double maxWorkhours = lstPp.stream().mapToDouble(MesProductProcedureVo::getBottleneckProcedureWorkHours).max().getAsDouble();
        //按瓶颈工序最长的反比的比例分配工序线产量，先计算最少产量的工序产量值
        Double totalPercent =  lstPp.stream().map(o-> {
             Float f = o.getBottleneckProcedureWorkHours();
             o.setBottleneckProcedureWorkHours(Convert.toFloat(maxWorkhours/f));
             return o;
        }).mapToDouble(o->o.getBottleneckProcedureWorkHours()).sum();

        HashMap<Long,Integer> map = new HashMap<>();
        for (MesProductProcedureVo mesProductProcedureVo : lstPp) {
            map.put(mesProductProcedureVo.getId(),Convert.toInt(Math.ceil(qty/totalPercent*mesProductProcedureVo.getBottleneckProcedureWorkHours())));
        }
        //判断是否超产，如果是从最多的产线上减去超出部分
        Integer totalQty = 0;
        for (Integer itm: map.values()
             ) {
              totalQty += itm;
        }
        Integer outerQty = Convert.toInt(totalQty- qty);
        if(outerQty> 0)
        {
             //找出map中value最大的。
             Integer maxVal = map.values().stream().mapToInt(o->o).max().getAsInt();
            for (Map.Entry<Long,Integer> itm : map.entrySet()
                 ) {
                if(itm.getValue().equals(maxVal))
                {
                    itm.setValue(maxVal - outerQty);
                }
            }
        }
        return map;
    }


}
